#pragma once
/**
 * @author LIU YUAN
 * @date May 15, 2011 11:12:21 AM
 * @param <T>
 */
#include "Common.h"
#include <winternl.h>  

typedef struct MODULE_INFO  {  
  DWORD address;
  DWORD moduleNameLength;
  WCHAR moduleName[MAX_PATH*2+4];
} *PMODULE_INFO;  

typedef struct tagWNDINFO{
	DWORD dwProcessId;
	HWND hWnd;
	HWND parent;
	LPCSTR className;
	BOOL isWindowVisible;
	HWND hWnds[MAX_PATH];
	DWORD hWndsSize;
	BOOL isSingleton;
} WNDINFO, *LPWNDINFO;



class LYLIB_API CInjectUtils
{
public:
	CInjectUtils(void);
	~CInjectUtils(void);
	
	static
    void
	PrintLastErrorMessage(DWORD dw);

	static
	DWORD
	GetProcessIdByName(LPCWSTR processName);

	static
	BOOL
	GetSimpleProcessNameById(DWORD pid,LPSTR lpName,PDWORD cbName);

	static
	HMODULE
	GetModule(
			DWORD processId,
			LPCWSTR moduleName);

	static
	FARPROC 
    GetFunctionAddr(
            LPCSTR moduleName,
			LPCSTR functionName);

	static
	WCHAR*
	getWideString(CHAR* str);

	static
	CHAR*
	getMultiString(WCHAR* str);

	static
	BOOL
	getMethodOffsetInDll(
                LPCSTR dllname,
		        LPCSTR method,
				DWORD* offset);
	static
	DWORD
	getEntryPoint(DWORD processId);

	static
	BOOL
	enumProcessForModule(DWORD processId,PMODULE_INFO lpModuleInfo,DWORD cb,LPDWORD lpCbNeeded,LPCWSTR moduleName=NULL);

	static
	BOOL
	injectCodesToProcess(DWORD processId,LPCSTR shellCodes,DWORD nSize);

	static
	DWORD
	getOrigCodesSize(DWORD processId,LPCSTR origCodesAddr,DWORD hookCodesSize);

	static
	HWND
	getSingletonWindow(DWORD processId,HWND parent = 0,LPCSTR className = 0,BOOL isWindowVisible = TRUE);

	static
	BOOL
	getMultipleWindows(LPWNDINFO wi);

	static
    BOOL
	sendMessage(DWORD pid,HWND hWnd,UINT msg,PVOID lpData,DWORD cbData);

	static
    BOOL
	postMessage(DWORD pid,HWND hWnd,UINT msg,PVOID lpData,DWORD cbData);

	static
    BOOL
	GetRemoteData(DWORD pid,UINT msg,__in PVOID inData,__in DWORD inCbData,__out PVOID lpData,__in DWORD cbData);

	static
	BOOL
	sendStringIme(DWORD processId,LPCWSTR text);

	static
    BOOL
	ActiveWindow(HWND hWnd);

	static
    HKL
	InstallIme();

	static
    BOOL
	UninstallIme();

	static
    BOOL
	UnloadIme(DWORD pid,HKL hkl);

	static
    BOOL
	UnloadIme2(DWORD pid,HKL hkl);

	static
    HMODULE
	LoadDll(DWORD pid,LPCSTR dllname,LPCSTR method = 0);

	static
    BOOL
	FreeDll(DWORD pid,LPCSTR dllname);

	static
    HKL
	GetImeByName(LPCSTR imeName);

	static
    BOOL
	InvokeDllMethod(DWORD pid,LPCSTR dllName,LPCSTR method,DWORD paramNum = 0,LPVOID param = 0,DWORD paramSize = 0);

	static
	HMODULE
	GetModule(DWORD pid,LPSTR moduleName);
};

